import axios from 'axios'
import { Message } from 'element-ui'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})
// axios设置请求拦截器
service.interceptors.request.use(
  config => {
    return config
  }, err => {
    console.log(err)
    return Promise.reject(err)
  })

// axios设置响应拦截器
service.interceptors.response.use(response => {
  const { success, message, data } = response.data
  if (success) {
    return data
  } else {
    Message.error(message)
    return Promise.reject(message)
  }
}, err => {
  console.log(err + 'err')
  return Promise.reject(err)
})
export default service
